package com.hyjy.music.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

typealias OnLetterCheckedListener = String.() -> Unit

class SearchBar : View {
    constructor(context: Context?) : super(context) {
        SearchBar(context, null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        SearchBar(context, attrs, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    //上一个被选中的字母的索引值
    private var lastIndex = -1

    private val bounds = Rect()

    private val letters = arrayOf(
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
        "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    )

    private var paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        // 画笔文本加粗
        typeface = Typeface.DEFAULT_BOLD
        // 设置字体颜色
        color = Color.BLACK
        // 设置字体大小
        textSize = 35.0f
    }

    // 单元格宽度
    private var cellWidth = 0f

    // 单元格高度
    private var cellHeight = 0f

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //初始化单元格的宽高
        cellWidth = measuredWidth.toFloat()
        val height = measuredHeight.toFloat()
        cellHeight = height / letters.size
    }

    override fun onDraw(canvas: Canvas) {
        // 遍历26个英文字母, 计算坐标, 进行绘制
        var zm: String
        for (i in letters.indices) {
            zm = letters[i]
            // 计算x坐标
            val zmWidth = paint.measureText(zm)
            val x = cellWidth * 0.5f - zmWidth * 0.5f
            // 计算y坐标
            // 获取文本的矩形区域,数据放在bounds里
            paint.getTextBounds(zm, 0, zm.length, bounds)
            val y = cellHeight * 0.5f + bounds.height() * 0.5f + i * cellHeight
            // 被选中字母的颜色与其他不同
            paint.color = if (lastIndex == i) Color.GREEN else Color.WHITE
            // 绘制文本
            canvas.drawText(zm, x, y, paint)
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> handleMotionEvent(event)
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL ->
                // 抬手时重新给赋值为-1
                lastIndex = -1
            else -> {}
        }
        //重绘改变被选中字母的颜色
        invalidate()
        return true
    }

    private fun handleMotionEvent(event: MotionEvent) {
        val y: Float = event.y
        val currentIndex = (y / cellHeight).toInt()
        if (currentIndex == lastIndex) {
            return
        }
        if (currentIndex >= 0 && currentIndex < letters.size) {
            // 防止数组越界异常,这个判断不能少.
            onLetterCheckedListener?.invoke(letters[currentIndex])
            // 记录上一次触摸的字母
            lastIndex = currentIndex
        }
    }

    private var onLetterCheckedListener: OnLetterCheckedListener? = null

    fun setOnLetterCheckedListener(onLetterCheckedListener: OnLetterCheckedListener) {
        this.onLetterCheckedListener = onLetterCheckedListener
    }
}